package com.kettle.controller;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kettle.controllerapis.DatalogControllerApi;
import com.kettle.model.datalog.Datalog;
import com.kettle.model.quartz.ReturnResult;
import com.kettle.model.rowpage.PageResult;
import com.kettle.service.datalog.DatalogService;
/**
 * 日志信息 控制层
 * @author king
 *
 */
@RestController
@RequestMapping("/datalogs")
public class DatalogController implements DatalogControllerApi{

	@Resource
    private DatalogService datalogService;
	
	@Override
	@RequestMapping(value="searchlogslist",method = RequestMethod.POST)
    public PageResult logslist(Datalog datalog,Integer page, Integer rows){
		QueryWrapper<Datalog> entityWrapper = new QueryWrapper<>();
        if(!"".equals(datalog.getJobname()) && datalog.getJobname()!=null){
            entityWrapper.like("jobname",datalog.getJobname());
        }
        PageResult result = new PageResult(); 
		PageInfo<Datalog> pageInfo; 
	    try{
	    	if(rows==null) {
	    		rows=10;
	    	}
	    	PageHelper.startPage(page,rows,"kssj desc");
	    	List<Datalog> list= datalogService.list(entityWrapper);
	        pageInfo = new PageInfo<Datalog>(list);
	        result.setTotal(pageInfo.getTotal());
	        result.setRows(list);
	    } finally {
	        PageHelper.clearPage();
	    }
		return result;
	}
	
	@Override
	@RequestMapping(value="savelogs",method = RequestMethod.POST)
	public ReturnResult savelogs(Datalog datalog){
		ReturnResult kmap=new ReturnResult();
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.replace("-", "");
		datalog.setId(uuid);
		boolean issave=datalogService.save(datalog);
		if(issave){
			kmap.setCode("500");
			kmap.setMsg("保存失败，请重试");
			kmap.setSuccess(false);
		}else{
			kmap.setCode("200");
			kmap.setMsg("保存成功");
			kmap.setSuccess(true);
		}
		return kmap;
	}
	
	@Override
	@RequestMapping(value="findlogs",method = RequestMethod.POST)
	public Datalog findlogs(String xh){
		Datalog datalog=datalogService.getById(xh);
		return datalog;
	}
}
